<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="校验类型" prop="checkCode">
        <el-select clearable v-model="queryParams.checkCode"
                   placeholder="请选择校验类型">
          <el-option
            v-for="dict in listCheckTableCode"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="表名前缀" prop="tableNamePrefix">
        <el-input
          v-model="queryParams.tableNamePrefix"
          placeholder="请输入表名前缀"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名后缀" prop="tableNameSuffix">
        <el-input
          v-model="queryParams.tableNameSuffix"
          placeholder="请输入表名后缀"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名包含" prop="tableNameContain">
        <el-input
          v-model="queryParams.tableNameContain"
          placeholder="请输入表名包含"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:tableStandardcheck:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reportworld:tableStandardcheck:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:tableStandardcheck:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:tableStandardcheck:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" v-loading="loading" max-height="550px" :data="tableStandardcheckList"
              @selection-change="handleSelectionChange" @row-click="rowClick"  ref="tableStandardcheck"
              @sort-change="handleSortChange" :row-style="tableRowStyle">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column  v-hasPermi="['reportworld:tableStandardcheck:edit']"
          label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleUpdate(scope.row,false)"
                v-hasPermi="['reportworld:tableStandardcheck:query']"
              >详情</el-button>
            </template>
        </el-table-column>
      <el-table-column label="表名前缀" align="center" prop="tableNamePrefix" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="表名后缀" align="center" prop="tableNameSuffix" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="表名包含" align="center" prop="tableNameContain" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="校验类型" align="center" prop="checkCode" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
      <template slot-scope="scope">
        <span v-show="scope.row.checkCode == 'LAYERED'">按照表分层</span>
        <span v-show="scope.row.checkCode == 'CUSTOMER'">按照客户编码</span>
        <span v-show="scope.row.checkCode == 'REPORT'">按照报表编码</span>
        <span v-show="scope.row.checkCode == 'MODULAR'">按照模块编码</span>
      </template>
      </el-table-column>
      <el-table-column label="类型对应值" align="center" prop="checkValue" width="180" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改校验规则对话框 -->
    <el-dialog  :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="校验类型" prop="checkCode">
              <el-select clearable style="width: 100%" v-model="form.checkCode"
                         placeholder="请选择校验类型">
                <el-option
                  v-for="dict in listCheckTableCode"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名前缀" prop="tableNamePrefix">
              <el-input clearable v-model="form.tableNamePrefix" placeholder="请输入表名前缀"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名后缀" prop="tableNameSuffix">
              <el-input clearable v-model="form.tableNameSuffix" placeholder="请输入表名后缀"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名包含" prop="tableNameContain">
              <el-input clearable v-model="form.tableNameContain" placeholder="请输入表名包含"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在分层" prop="layered"
                          v-if="form.checkCode === 'LAYERED' ? true:false">
              <el-select clearable @change="$forceUpdate()" v-model="form.layered" placeholder="请选择所在分层">
                <el-option
                  v-for="dict in dict.type.report_table_layered"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属模块" prop="strModular"
                          v-if="form.checkCode === 'MODULAR' ? true:false">
              <treeselect  v-model="form.strModular"
                          :show-count="true"
                          :multiple="false"
                          :disable-branch-nodes="true"
                          :options="modularTree"
                          placeholder="请选择所属模块"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属客户" prop="strCustomer"
                          v-if="form.checkCode === 'CUSTOMER' ? true:false">
              <treeselect  v-model="form.strCustomer"
                          :show-count="true"
                          :multiple="false"
                          :disable-branch-nodes="true"
                          :options="customerTree"
                          placeholder="请选择所属客户"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属报表" prop="strReport"
                          v-if="form.checkCode === 'REPORT' ? true:false">
              <treeselect  v-model="form.strReport"
                          :show-count="true"
                          :multiple="false"
                          :disable-branch-nodes="true"
                          :options="reportTree"
                          placeholder="请选择所属报表"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input maxlength="500" clearable v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTableStandardcheck, getTableStandardcheck, delTableStandardcheck, addTableStandardcheck, updateTableStandardcheck,init } from "@/api/reportworld/tableStandardcheck";
import { getTableRowStyle } from "@/utils/yss";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "TableStandardcheck",
  components: {
    Treeselect
  },
  dicts: ['sys_common_status','report_table_layered'],
  data() {
    return {
      butloading:false,
      isUpdate: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 校验规则表格数据
      tableStandardcheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 校验类型
      listCheckTableCode:[],
      //所属模块
      modularTree : [],
      // 所属客户
      customerTree: [],
      //所属报表
      reportTree: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableNamePrefix: null,
        tableNameSuffix: null,
        tableNameContain: null,
        status: null,
        checkCode: null,
        checkValue: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkCode: [
          { required: true, message: "校验类型不能为空", trigger: "blur" }
        ],
        layered: [
          { required: true, message: "所在分层不能为空", trigger: "blur" }
        ],
        strModular: [
          { required: true, message: "所属模块不能为空", trigger: "blur" }
        ],
        strCustomer: [
          { required: true, message: "所属客户不能为空", trigger: "blur" }
        ],
        strReport: [
          { required: true, message: "所属报表不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.init();
    this.getList();
  },
  methods: {
    /** 查询校验规则列表 */
    getList() {
      this.loading = true;
      listTableStandardcheck(this.queryParams).then(response => {
        this.tableStandardcheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableNamePrefix: null,
        tableNameSuffix: null,
        tableNameContain: null,
        status: null,
        checkCode: null,
        checkValue: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
      /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
     },
    init(){ //初始化加载校验类型
      init().then(response => {
        this.listCheckTableCode=response.listCheckTableCode;
        this.modularTree=response.modularTree;
        this.customerTree=response.customerTree;
        this.reportTree=response.reportTree;
    });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加校验规则";
      this.isUpdate =true;
      this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
    },
    /** 修改按钮操作 */
    handleUpdate(row,isUpdate) {
      this.reset();
      const id = row.id || this.ids
      getTableStandardcheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改校验规则";
      this.isUpdate =true;
      if(isUpdate == false){
        this.title = "表改校验规则详情";
        this.isUpdate =false;
      }
      if ('MODULAR' == response.data.checkCode) {
        this.form.strModular = response.data.checkValue;
      }
      if ('CUSTOMER' == response.data.checkCode) {
        this.form.strCustomer = response.data.checkValue;
      }
      if ('REPORT' == response.data.checkCode) {
        this.form.strReport = response.data.checkValue;
      }
      if ('LAYERED' == response.data.checkCode) {
        this.form.layered = parseInt(response.data.checkValue);
      }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.tableNamePrefix == '' && this.form.tableNameSuffix == '' && this.form.tableNameContain == ''){
            this.$modal.msgError("表名前缀，表名后缀，表名包含，至少有一个不能为空");
            return;
          }
          if ('MODULAR' == this.form.checkCode) {
            this.form.checkValue= this.form.strModular;
          }
          if ('CUSTOMER' == this.form.checkCode) {
            this.form.checkValue=this.form.strCustomer;
          }
          if ('REPORT' == this.form.checkCode) {
            this.form.checkValue=this.form.strReport;
          }
          if ('LAYERED' == this.form.checkCode) {
            this.form.checkValue=this.form.layered;
          }
          this.butloading=true;
          if (this.form.id != null) {
            updateTableStandardcheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
          } else {
            addTableStandardcheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delTableStandardcheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids=JSON.stringify(this.ids);
      this.download('reportworld/tableStandardcheck/export', {
        ...this.queryParams
      }, `tableStandardcheck_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    rowClick(row, column, event) { //单击行
      this.$refs.tableStandardcheck.toggleRowSelection(row);
    }
  }
};
</script>
